package com.tgpms.web.assets.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.tgpms.exception.*;
import com.tgpms.util.PageView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.tgpms.auth.security.TokenService;
import com.tgpms.common.Result;
import com.tgpms.util.Common;
import com.tgpms.util.PageInfo;
import com.tgpms.web.assets.entity.FaTransfer;
import com.tgpms.web.assets.service.FaTransferService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author newway
 * <p>
 * D8003固定资产转移接口
 */
@Api(value = "FaTransferController", tags = "固定资产转移接口")
@RestController
@RequestMapping(value = "/ev/faTransfer")
public class FaTransferController {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private FaTransferService faTransferService;
	@Autowired
	private TokenService tokenService;


	/**
	 * @Author newway
	 * @Description 查询资产转移集合
	 * @Date 2019年8月31日上午10:32:12
	 * @Param 
	 * @return 
	 **/
	@SuppressWarnings("unchecked")
	@ApiOperation(value = "查询固定资产转移接口信息", notes = "查询固定资产转移接口信息", httpMethod = "POST")
	@PostMapping(value = "/selFaTransferList")
	public Result selFaTransferList(@RequestParam(value = "selparam", required = false) String selparam,
			@RequestParam(value = "leftdition", required = false) String leftdition,
			@RequestParam(value = "condition", required = false) String condition,
			@RequestParam(value = "dataowner", required = false) String dataowner,
			@RequestParam(value = "pageNo", required = true) String pageNo,
			@RequestParam(value = "pageSize", required = true) String pageSize){
		Result result = new Result();
		PageInfo pageInfo = new PageInfo();
		if(Common.isEmpty(pageNo) || Common.isEmpty(pageSize)) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
			return result;
		}
		try {
			pageInfo.setPage(pageNo);
			pageInfo.setSize(pageSize);
			Map<String,String> map = new HashMap<String,String>();
			map.put("selparam", selparam);
			map.put("leftdition", leftdition);
			map.put("condition", condition);
			map.put("dataowner", dataowner);
			pageInfo.setQueryMap(map);
			result = faTransferService.selFaTransferList(pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_QUERY.getExplain());
			throw new QueryException(ExceptionExplain.ERROR_BY_QUERY.getExplain());
		}
		return result;
	}

	/**
	 * @Author newway
	 * @Description 添加固定资产转移接口信息
	 * @Date 2019年8月1日下午4:22:08
	 * @Param com.tgpms.web.assets.entity.FaTransfer
	 * @return com.tgpms.common.Result
	 **/
	@ApiOperation(value = "添加固定资产转移接口信息", notes = "添加固定资产转移接口信息", httpMethod = "POST")
	@PostMapping(value = "/addFaTransferInfo")
	public Result addFaTransferInfo(@RequestBody FaTransfer FaTransfer,HttpServletRequest request) {
		Result result = new Result();
		if(Common.isEmpty(FaTransfer.getFixedAssetNo())) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
			return result;
		}
		try {
			//获取token操作人
			FaTransfer.setInsertUser(tokenService.getUserName(request));
			FaTransfer.setInsertDate(new Date());
			Map<String, String> map = faTransferService.addFaTransferInfo(FaTransfer);
			if(map.get("code").equals("1")) {
				result.setSuccess(true);
				result.setData(map.get("data"));
			}else {
				result.setSuccess(false);
			}
			result.setMsg(map.get("info"));
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
			throw new InsertException(ExceptionExplain.ERROR_BY_INSERT.getExplain());
		}
		return result;
	}


	/**
	 * @Author newway
	 * @Description 更新固定资产转移接口信息
	 * @Date 2019年8月1日下午5:37:08
	 * @Param com.tgpms.web.assets.entity.FaTransfer
	 * @return com.tgpms.common.Result
	 **/
	@ApiOperation(value = "更新固定资产转移接口信息", notes = "更新固定资产转移接口信息", httpMethod = "PUT")
	@PutMapping(value = "/updateFaTransferInfo")
	public Result updateFaTransferInfo(@RequestBody FaTransfer FaTransfer,HttpServletRequest request) {
		Result result = new Result();
		if(Common.isEmpty(FaTransfer.getFixedAssetNo()) || Common.isEmpty(FaTransfer.getFaTransferNo())) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
			return result;
		}
		try {
			//获取token操作人
			FaTransfer.setModifyUser(tokenService.getUserName(request));
			FaTransfer.setModifyDate(new Date());
			Map<String, String> map = faTransferService.updateFaTransferInfo(FaTransfer);
			if(map.get("code").equals("1")) {
				result.setSuccess(true);
			}else {
				result.setSuccess(false);
			}
			result.setMsg(map.get("info"));
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
			throw new UpdateException(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
		}
		return result;
	}


	/**
	 * @Author newway
	 * @Description 删除固定资产转移接口信息
	 * @Date 2019年8月1日下午5:41:57
	 * @Param com.tgpms.web.assets.entity.FaTransfer
	 * @return com.tgpms.common.Result
	 **/
	@ApiOperation(value = "删除固定资产转移接口信息", notes = "删除固定资产转移接口信息", httpMethod = "DELETE")
	@DeleteMapping(value = "/deleteFaTransferInfo")
	public Result deleteFaTransferInfo(@RequestParam(value = "faTransferNo", required = true) String faTransferNo,
			@RequestParam(value = "fixedAssetNo", required = true) String fixedAssetNo) {
		Result result = new Result();
		if(Common.isEmpty(faTransferNo) || Common.isEmpty(fixedAssetNo)) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
			return result;
		}
		try {
			Map<String, String> map = faTransferService.deleteFaTransferInfo(faTransferNo,fixedAssetNo);
			if(map.get("code").equals("1")) {
				result.setSuccess(true);
			}else {
				result.setSuccess(false);
			}
			result.setMsg(map.get("info"));
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
			throw new DeleteException(ExceptionExplain.ERROR_BY_DELETE.getExplain());
		}
		return result;
	}


	/**
	 * @Author newway
	 * @Description 更新固定资产转移状态接口
	 * @Date 2019年8月13日上午10:56:41
	 * @Param faTransferNo 转移序号
	 * @Param fixedAssetNo 资产编号
	 * @return com.tgpms.common.Result
	 **/
	@ApiOperation(value = "更新固定资产转移状态接口", notes = "更新固定资产转移状态接口", httpMethod = "POST")
	@PostMapping(value = "/updateFaTransferState")
	public Result updateFaTransferState(HttpServletRequest request,
			@RequestParam(value = "faTransferNo", required = true) String faTransferNo,
			@RequestParam(value = "fixedAssetNo", required = true) String fixedAssetNo,
			@RequestParam(value = "dataOwner", required = true) String dataOwner) {
		Result result = new Result();
		//
		try {
			FaTransfer faTransfer = new FaTransfer();
			//获取token操作人
			faTransfer.setModifyUser(tokenService.getUserName(request));
			faTransfer.setModifyDate(new Date());
			faTransfer.setFaTransferNo(faTransferNo);
			faTransfer.setFixedAssetNo(fixedAssetNo);
			faTransfer.setDataOwner(dataOwner);
			faTransfer.setTransStatus("A");
			Map<String, String> map = faTransferService.updateFaTransferInfo(faTransfer);
			if(map.get("code").equals("1")) {
				result.setSuccess(true);
			}else {
				result.setSuccess(false);
			}
			result.setMsg(map.get("info"));
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
			throw new UpdateException(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
		}
		return result;

	}
}
